{include file="common/header"/}

<body class="bg-light">
	<div class="container">
		<div class="col-md-8">
			<div class="card my-4">
				<div class="card-body">
					<h4 class="card-title">发布内容</h4>
			    	<hr>			    	
					<textarea id="text_editor"></textarea>
					<button id="publish_submit" class="btn btn-primary mt-3">立即发布</button>
				</div>
			</div>
		</div>
	</div>
</body>

<script type="text/javascript">
	$(function() {
		var tinymce_config = {
		    selector: '#text_editor',
		    language:'zh_CN',
		    height: 300,
		    menubar: false,
		    branding: false,
		    plugins: 'lists, advlist, image, codesample, link, preview, emoticons, wordcount, noneditable, atsomeone',
		    toolbar: [
		    	'undo redo restoredraft | image codesample link atsomeone | alignleft aligncenter alignright alignjustify | bullist numlist | preview',
		    	'fontsizeselect | bold italic underline blockquote strikethrough emoticons'
		    ],
		    image_description: false,
		    images_upload_handler: function (blobInfo, successFunction, failFunction) {
		        var image_file = blobInfo.blob();
		        var formData = new FormData();
		        formData.append('image', image_file, image_file.name);

		    	$.ajax("/index/publish/uploadImage", {
		    		method: 'POST',
					data: formData,
					processData: false,
	    			contentType: false,
					success: function(result) {
						if (result.status == 'success') {
							toastr.success(result.message);
							successFunction('__IMAGE__' + '/' + result.data);
			            }
			            if (result.status == 'error') {
			            	toastr.error(result.message);
			            	console.log(result.data);
			                failFunction(result.data);
			            }
					}
		    	});
    		},
    		codesample_languages: [
		        {text: 'Python', value: 'python'}
	   		],
	   		link_title: false,
	   		target_list: false,
	   		default_link_target: "_blank",
	   		autosave_ask_before_unload: false,
	   		fontsize_formats: '8px 10px 12px 14px 16px 18px',
	   		convert_urls: false
		}

		var admin = {$Think.session.admin};
		if (admin == 1) {
			tinymce_config.plugins += ', code';
			tinymce_config.toolbar[1] += ' code';
		}
		tinymce.init(tinymce_config);
		
		$('#publish_submit').click(function() {
			var editor = tinymce.editors['text_editor'];
			var content = editor.getContent();

			if (content == '') {
				toastr.warning('发布内容不能为空！');
				return ;
			}
			
			// 图片检测
			var img_sign = '<img';
			var img_num = 0;
			var img_index = content.indexOf(img_sign);
			var content_ = content;
			while (img_index != -1) {
				img_num++;
				content_ = content_.substring(img_index + img_sign.length);
				img_index = content_.indexOf(img_sign);
			}

			if (img_num > 3) {
				toastr.warning('发布内容不能存在超过3张图片！');
				return ;
			}

			// 字数检测
			var wordcount_plugin = editor.plugins.wordcount;
			var character_count = wordcount_plugin.body.getCharacterCount();
			if (admin != 1 && character_count > 1500) {
				toastr.warning('字符数不能超过1500！');
				return ;
			}

			// @ 检测
			var if_at = false;
			var at_list = [];
			var at_sign = 'at_user_id';
			var at_index = content.indexOf(at_sign);
			var content_ = content;
			while (at_index != -1) {
				if_at = true;
				let at_end = content_.indexOf('"', at_index);
				let at_id = content_.substring(at_index + at_sign.length + 1, at_end);
				at_list.push(at_id);
				content_ = content_.substring(at_index + at_sign.length);
				at_index = content_.indexOf(at_sign);
			}

			$.post("/index/publish/addContent", {
				'content': content,
				'if_at': if_at,
				'at_list': at_list
			},
			function(result) {
				if (result.status == 'success') {
					toastr.success(result.message);
      				setTimeout(function(){
      					$(location).attr('href', result.data);
					}, 1000);
      			}
				if (result.status == 'error') {
      				toastr.error(result.message);
      			}
			}, "json");
		});
	});
</script>